﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.Modeling;

namespace DSLFactory.VSCTDesigner
{
    partial class ButtonSerializerBase
    {
        protected void CustomReadElements(SerializationContext serializationContext, ModelElement element, global::System.Xml.XmlReader reader)
        {
            base.ReadElements(serializationContext, element, reader);
        }

        [global::System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity", Justification = "Generated code.")]
        [global::System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1506:AvoidExcessiveClassCoupling", Justification = "Generated code.")]
        protected  void CustomWriteElements(SerializationContext serializationContext, ModelElement element, global::System.Xml.XmlWriter writer)
        {
            base.WriteElements(serializationContext, element, writer);
        }
    }

    partial class ButtonSerializer
    {
        #region CommandFlags
        protected override void ReadElements(SerializationContext serializationContext, ModelElement element, global::System.Xml.XmlReader reader)
        {
            CustomReadElements(serializationContext, element, reader);

            Button instanceOfCommandWithIcon = element as Button;
            global::System.Diagnostics.Debug.Assert(instanceOfCommandWithIcon != null, "Expecting an instance of CommandWithIcon!");

            // Read properties serialized as nested XML elements.
            if( !serializationContext.Result.Failed )
                CustomReadPropertiesFromElements(serializationContext, instanceOfCommandWithIcon, reader);
        }

        /// <summary>
        /// Reads the properties from elements.
        /// </summary>
        /// <param name="serializationContext">The serialization context.</param>
        /// <param name="element">The element.</param>
        /// <param name="reader">The reader.</param>
        /// <remarks>
        /// </remarks>
        protected void CustomReadPropertiesFromElements(Microsoft.VisualStudio.Modeling.SerializationContext serializationContext, Button element, System.Xml.XmlReader reader)
        {
            while( !serializationContext.Result.Failed && !reader.EOF && reader.NodeType == global::System.Xml.XmlNodeType.Element )
            {
                switch( reader.LocalName )
                {
                    case "VisibilityContext":	// VisibilityContext
                        if( reader.IsEmptyElement )
                        {	// No serialized value, must be default one.
                            SerializationUtilities.Skip(reader);  // Skip this tag.
                        }
                        else
                        {
                            string strVisibilityContext = reader.ReadInnerXml();
                            global::System.String valueOfVisibilityContext;
                            if( SerializationUtilities.TryGetValue<global::System.String>(SerializationUtilities.UnescapeXmlString(strVisibilityContext), out valueOfVisibilityContext) )
                            {
                                element.VisibilityContext = valueOfVisibilityContext;
                            }
                            else
                            {	// Invalid property value, ignored.
                                VSCTDesignerSerializationBehaviorSerializationMessages.IgnoredPropertyValue(serializationContext, reader, "VisibilityContext", typeof(global::System.String), strVisibilityContext);
                            }

                            SerializationUtilities.SkipToNextElement(reader);
                        }
                        break;
                    case "CommandFlag":	// CommandFlag
                        if( reader.IsEmptyElement )
                        {	// No serialized value, must be default one.
                            SerializationUtilities.Skip(reader);  // Skip this tag.
                        }
                        else
                        {
                            string strCommandFlag = reader.ReadInnerXml();
                            ButtonCommandFlag valueOfCommandFlag;
                            if( SerializationUtilities.TryGetValue<ButtonCommandFlag>(SerializationUtilities.UnescapeXmlString(strCommandFlag), out valueOfCommandFlag) )
                            {
                                element.CommandFlag |= valueOfCommandFlag;
                            }
                            else
                            {	// Invalid property value, ignored.
                                VSCTDesignerSerializationBehaviorSerializationMessages.IgnoredPropertyValue(serializationContext, reader, "CommandFlag", typeof(ButtonCommandFlag), strCommandFlag);
                            }

                            SerializationUtilities.SkipToNextElement(reader);
                        }
                        break;
                    default:
                        return;  // Don't know this element.
                }
            }
        }

        protected override void WriteElements(SerializationContext serializationContext, ModelElement element, global::System.Xml.XmlWriter writer)
        {
            CustomWriteElements(serializationContext, element, writer);

            Button instance = element as Button;
            global::System.Diagnostics.Debug.Assert(instance != null, "Expecting an instance of CommandWithIcon!");

            // Write properties serialized as nested XML elements.
            if( !serializationContext.Result.Failed )
                CustomWritePropertiesAsElements(serializationContext, instance, writer);
        }

        /// <summary>
        /// Writes the properties as elements.
        /// </summary>
        /// <param name="serializationContext">The serialization context.</param>
        /// <param name="element">The element.</param>
        /// <param name="writer">The writer.</param>
        /// <remarks>
        /// </remarks>
        protected void CustomWritePropertiesAsElements(Microsoft.VisualStudio.Modeling.SerializationContext serializationContext, Button element, System.Xml.XmlWriter writer)
        {
            // VisibilityContext
            if( !serializationContext.Result.Failed )
            {
                global::System.String propValue = element.VisibilityContext;
                if( !serializationContext.Result.Failed )
                {
                    if( !string.IsNullOrEmpty(propValue) )
                        writer.WriteElementString("VisibilityContext", propValue);
                }
            }
            // CommandFlag
            if( !serializationContext.Result.Failed )
            {
                foreach( ButtonCommandFlag value in Enum.GetValues(typeof(ButtonCommandFlag)) )
                {
                    if( ( element.CommandFlag & value ) == value )
                    {
                        string serializedPropValue = SerializationUtilities.GetString<ButtonCommandFlag>(serializationContext, value);
                        if( !serializationContext.Result.Failed )
                        {
                            writer.WriteElementString("CommandFlag", serializedPropValue);
                        }
                    }
                }
            }
        }
        #endregion
    }
}
